import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Vector;


public class BP_OWN_2 {
	int outcome, prediction;
	private int correct;
	private int incorrect;

	private String[] items;
	private String BIA;
	private String BTA; 
	
	twoBitPredictor[] p;
	
	private int[] globalHistory = {0,0};

	public BP_OWN_2() {
		p = new twoBitPredictor[64];
	}
	public static void main(String[] args) throws IOException {

		BP_OWN_2 schd = new BP_OWN_2();

		newPredictor p1 = new newPredictor(0);
		
		for( int i=0; i<4; i++) {
			schd.p[i] = new twoBitPredictor(i);
		}
		
		Scanner s = null;
		try {
			s = new Scanner(new BufferedReader(
					new FileReader("inputFiles"+File.separatorChar+"history.txt")));
		} catch (FileNotFoundException e) {
			//e.printStackTrace();
			System.out.println("read file...  " + 
			"input File not found exception");
			System.exit(0);
		}
		if (s == null) {
			System.exit(0);
		}

		while (s.hasNext()) {
			String str = s.nextLine();
			schd.items = str.split("\t");
			schd.BIA = schd.items[0].trim();
			schd.BTA = schd.items[1].trim();
			schd.outcome = (schd.items[2].charAt(0) == '0' ? 0 : 1);
			
			int index=0;
			
			index = schd.p[0].add(schd.BIA, schd.BIA);
			index = schd.p[1].add(schd.BIA, schd.BIA);
			index = schd.p[2].add(schd.BIA, schd.BIA);
			index = schd.p[3].add(schd.BIA, schd.BIA);
			
			if(schd.globalHistory[0] == 0 &&
					schd.globalHistory[1] ==0) {
				//index = schd.p[0].add(schd.BIA, schd.BIA);
				//schd.p[0].predict(schd.BIA, schd.BTA, index);
				//schd.p[0].update(index, schd.outcome);
				
			} 
			
			
			index = p1.add(schd.BIA, schd.BTA);
			schd.prediction = p1.predict(schd.BIA, schd.BTA, index);
			p1.update(index, schd.outcome);
			
			if(schd.prediction == schd.outcome) {
                schd.correct++;
            } else {
            	schd.incorrect++;
            }
			
			
			
			schd.globalHistory[1] = schd.globalHistory[0];
			schd.globalHistory[0] = schd.outcome;
			
		}

		float result = ((float)schd.correct/(float)(schd.correct+schd.incorrect))*100;
		System.out.format("%.3f\n", result);

		System.out.println ("correct prediction = " + schd.correct);
		System.out.println ("incorrect prediction = " + schd.incorrect);

	}
}

class BT {
	String BIA;
	String BTA;
	int bit0=0, bit1=0;
}
